/*
 * Copyright (c) 2010 Mathias Garbe <code@mathias-garbe.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef SETTINGSWINDOW_HPP
#define SETTINGSWINDOW_HPP

#include <QtGui/QMainWindow>
#include <QApplication>
#include <QDesktopWidget>
#include <QCloseEvent>
#include <QMessageBox>
#include <QFileDialog>
#include <QStandardItemModel>
#include <QModelIndexList>

#include "ui_SettingsWindow.h"

#include "MainWindow.hpp"
#include "GameInfo.hpp"
#include "AddPathWindow.hpp"

class MainWindow;
class AddPathWindow;
class AdvancedGameInfo;

class SettingsWindow : public QDialog
{
    Q_OBJECT

public:
    SettingsWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~SettingsWindow();

    void show();


    bool closed;

private:
    Ui::SettingsForm ui;
    MainWindow *mainWindow;
    AddPathWindow *addPathWindow;

    void closeEvent(QCloseEvent *event);

    void loadSettings();
    std::string saveSettings();

    void rebuildTabs();

    // Advanced custom Paths
    std::vector<AdvancedGameInfo> customAdvPaths[5];
    QStandardItemModel *customAdvPathModel;

    void rebuildPathTree();

private slots:

    void applySettings();
    void closeSettings()
    {
        this->close();
    }

    void toggleAdvancedSettings(int dontCare)
    {
        this->rebuildTabs();
    }


    void customPathAdvTabChanged(int dontCare)
    {
        this->rebuildPathTree();
    }

    void addCustomPath();
    void editCustomPath();
    void delCustomPath();

    // Maybe we can do this in a better way
    void setTDPath()
    {
        QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
        QString directory = QFileDialog::getExistingDirectory(this, tr("Please point me to the game directory..."),
                                                              "", options);
        if (!directory.isEmpty())
            this->ui.lineTD->setText(directory);
    }

    void setRAPath()
    {
        QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
        QString directory = QFileDialog::getExistingDirectory(this, tr("Please point me to the game directory..."),
            "", options);
        if (!directory.isEmpty())
            this->ui.lineRA->setText(directory);
    }

    void setTSPath()
    {
        QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
        QString directory = QFileDialog::getExistingDirectory(this, tr("Please point me to the game directory..."),
            "", options);
        if (!directory.isEmpty())
            this->ui.lineTS->setText(directory);
    }

    void setRA2Path()
    {
        QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
        QString directory = QFileDialog::getExistingDirectory(this, tr("Please point me to the game directory..."),
            "", options);
        if (!directory.isEmpty())
            this->ui.lineRA2->setText(directory);
    }

    void setRA2YRPath()
    {
        QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
        QString directory = QFileDialog::getExistingDirectory(this, tr("Please point me to the game directory..."),
            "", options);
        if (!directory.isEmpty())
            this->ui.lineRA2YR->setText(directory);
    }
};

#endif
